package com.sbhotel.chotellauncher.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;

import com.google.gson.Gson;
import com.sbhotel.chotellauncher.R;
import com.sbhotel.chotellauncher.constant.Constants;
import com.sbhotel.chotellauncher.entity.LauncherMainMenuBean;
import com.sbhotel.chotellauncher.util.GlideImageLoader;
import com.sbhotel.chotellauncher.util.HttpUtil;
import com.sbhotel.chotellauncher.util.LauncherConfigUtil;
import com.sbhotel.chotellauncher.util.Utils;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

public class ADActivity extends Activity {

    Banner banner;

    //int[] images = {R.mipmap.ad1, R.mipmap.ad2};
    List<Integer> images = new ArrayList<>();
    List<String> imgUrl = new ArrayList<>();
    List<Object> list = new ArrayList<>();
    Gson mGson = new Gson();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_ad);
        initData();
    }

    private void initData() {

        //images.add(R.mipmap.ad2);
        //images.add(R.mipmap.ad3);
        //images.add(R.mipmap.ad4);
        //images.add(R.mipmap.ad5);

        banner = (Banner) findViewById(R.id.banner);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        banner.setViewPagerIsScroll(false); //手动滑动
        banner.setDelayTime(10000);
        banner.isAutoPlay(true);
        //设置图片集合
        banner.setImages(initImages());
        //banner设置方法全部调用完毕时最后调用


        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                ADActivity.this.finish();
            }
        });
        banner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {

            }

            @Override
            public void onPageScrollStateChanged(int i) {
                Log.d(Constants.LOG_TAG, "onPageScrollStateChanged = " + i);
            }
        });
        banner.start();
        HttpUtil.dataReport(Constants.CODE_AD_DISPLAY, Utils.getDeviceUn(this));
    }

    private List<Object> initImages() {
        String configStr = LauncherConfigUtil.readFileData(this, Constants.CONFIG_FILE_OK_PATH);
        if(TextUtils.isEmpty(configStr)) {
            //images.add(R.mipmap.ad1);
            list.add(R.mipmap.ad1);
        } else {
            LauncherMainMenuBean bean = mGson.fromJson(configStr, LauncherMainMenuBean.class);
            for(int i = 0; i < bean.getData().getRotation().size(); i++) {
                list.add(bean.getData().getRotation().get(i).getImage_url());
            }
        }
        return list;
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        finish();
        return super.dispatchKeyEvent(event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return super.onKeyDown(keyCode, event);
    }
}
